

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>cogentviewer.models.location &mdash; Cogent Viewer 0.1 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/rtd.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Cogent Viewer 0.1 documentation" href="../../../index.html" />
    <link rel="up" title="Module code" href="../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for cogentviewer.models.location</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>

<span class="sd">.. codeauthor::  Ross Wiklins </span>
<span class="sd">.. codeauthor::  James Brusey</span>
<span class="sd">.. codeauthor::  Daniel Goldsmith &lt;djgoldsmith@googlemail.com&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">sqlalchemy</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">meta</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">Base</span>

<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Table</span><span class="p">,</span> <span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">String</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span><span class="n">Float</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">relationship</span><span class="p">,</span> <span class="n">backref</span>

<div class="viewcode-block" id="Location"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.location.Location">[docs]</a><span class="k">class</span> <span class="nc">Location</span><span class="p">(</span><span class="n">Base</span><span class="p">,</span><span class="n">meta</span><span class="o">.</span><span class="n">InnoDBMix</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Location provides a link between houses and rooms.</span>
<span class="sd">    This is needed if the node is moved, for example during multiple deployments.</span>
<span class="sd">    </span>
<span class="sd">    This table should be an &quot;associatve array&quot; transparrently linking houses and rooms</span>


<span class="sd">    :var Integer id: Id of Location</span>
<span class="sd">    :var Integer houseId: Link to :class:`cogentviewer.models.house.House` the room is in</span>
<span class="sd">    :var Integer roomId: Link to :class:`cogentviewer.models.room.Room` this location corresponds to</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&quot;Location&quot;</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">(</span>
        <span class="n">sqlalchemy</span><span class="o">.</span><span class="n">UniqueConstraint</span><span class="p">(</span><span class="s">&#39;houseId&#39;</span><span class="p">,</span> <span class="s">&#39;roomId&#39;</span><span class="p">),</span>
        <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span> <span class="s">&#39;InnoDB&#39;</span><span class="p">,</span>
         <span class="s">&#39;mysql_charset&#39;</span><span class="p">:</span><span class="s">&#39;utf8&#39;</span><span class="p">},</span>
        <span class="p">)</span>
   
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">houseId</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span>
                     <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;House.id&#39;</span><span class="p">))</span>
    <span class="n">roomId</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span>
                    <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;Room.id&#39;</span><span class="p">))</span>

    <span class="n">nodes</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Node&quot;</span><span class="p">,</span><span class="n">backref</span><span class="o">=</span><span class="n">backref</span><span class="p">(</span><span class="s">&#39;location&#39;</span><span class="p">))</span>
    <span class="n">readings</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Reading&quot;</span><span class="p">,</span><span class="n">backref</span><span class="o">=</span><span class="n">backref</span><span class="p">(</span><span class="s">&#39;location&#39;</span><span class="p">))</span>
    <span class="n">filtReadings</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Reading&quot;</span><span class="p">,</span><span class="n">lazy</span><span class="o">=</span><span class="s">&#39;dynamic&#39;</span><span class="p">)</span>

<div class="viewcode-block" id="Location.asJSON"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.location.Location.asJSON">[docs]</a>    <span class="k">def</span> <span class="nf">asJSON</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">parentId</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Differes from the standard asJSON model by returning the</span>
<span class="sd">        name of the room as its name</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">theItem</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;id&quot;</span><span class="p">:</span><span class="s">&quot;L_{0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">),</span>
                   <span class="s">&quot;name&quot;</span><span class="p">:</span><span class="s">&quot;{0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">room</span><span class="o">.</span><span class="n">name</span><span class="p">),</span>
                   <span class="s">&quot;label&quot;</span><span class="p">:</span><span class="s">&quot;({0}) {1}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">room</span><span class="o">.</span><span class="n">name</span><span class="p">),</span>
                   <span class="s">&quot;type&quot;</span><span class="p">:</span><span class="s">&quot;location&quot;</span><span class="p">,</span>
                   <span class="s">&quot;parent&quot;</span><span class="p">:</span> <span class="s">&quot;H_{0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">houseId</span><span class="p">),</span>
                   <span class="c">#&quot;children&quot;:[]</span>
                   <span class="p">}</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">theItem</span><span class="p">)</span>
        <span class="c">#log.info(self.readings[0])</span>
        
        <span class="k">try</span><span class="p">:</span>
            <span class="n">hasRead</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">readings</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">hasRead</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="c">#return None</span>
        <span class="c">#log.info(self.readings.count())</span>
        <span class="k">return</span> <span class="n">theItem</span>
</div>
<div class="viewcode-block" id="Location.getReadings"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.location.Location.getReadings">[docs]</a>    <span class="k">def</span> <span class="nf">getReadings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">typeId</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Attempt to return only readings of a certain type</span>

<span class="sd">        :param typeId: Type If of object to filter by</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">typeId</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">filtReadings</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">typeId</span> <span class="o">=</span> <span class="n">typeId</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">readings</span>
</div>
    <span class="k">def</span> <span class="nf">asTree</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">asJSON</span><span class="p">()</span>
        
    <span class="k">def</span> <span class="nf">asList</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">parentId</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">):</span>
        <span class="n">outDict</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">asJSON</span><span class="p">(</span><span class="n">parentId</span><span class="p">)]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
            <span class="n">outDict</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">&quot;children&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
                <span class="n">outDict</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">asList</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">outDict</span>


    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c">#return (&quot;Loc ({0}): House {1} , Room {2}&quot;.format(self.id,self.houseId,self.roomId))</span>
	<span class="k">return</span> <span class="s">&quot;({0}) {1} : {2}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">house</span><span class="o">.</span><span class="n">address</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">room</span><span class="o">.</span><span class="n">name</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, Daniel Goldsmith.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>